import os
import csv
import shutil

result_folder='results'#钙化分割结果文件
out_folder='钙化积分风险分类'#钙化积分风险分类总文件夹
cac_result_csv='results/heart_cac.csv'#冠脉钙化积分结果csv文件
with open(cac_result_csv, mode='r', newline='', encoding='utf-8') as ori_file:
    reader = csv.reader(ori_file)
    next(reader)
    for row in reader:
        file_name=row[0]
        result_path = os.path.join(result_folder, file_name)
        cac_outcome = row[1]
        cac_value=float(cac_outcome)
        name = cac_outcome + '_' + file_name
        if cac_value==0:
            out_folder_name='无钙化斑块'
            out_folder_name_path=os.path.join(out_folder,out_folder_name)
            if not os.path.exists(out_folder_name_path):
                os.makedirs(out_folder_name_path)
            new_path = os.path.join(out_folder_name_path, name)
            shutil.copytree(result_path,new_path)
        if 0<cac_value <=10:
            out_folder_name = '极低风险'
            out_folder_name_path = os.path.join(out_folder, out_folder_name)
            if not os.path.exists(out_folder_name_path):
                os.makedirs(out_folder_name_path)
            new_path = os.path.join(out_folder_name_path, name)
            shutil.copytree(result_path, new_path)
        if 10 < cac_value <= 100:
            out_folder_name = '轻度钙化，低至中度风险'
            out_folder_name_path = os.path.join(out_folder, out_folder_name)
            if not os.path.exists(out_folder_name_path):
                os.makedirs(out_folder_name_path)
            new_path = os.path.join(out_folder_name_path, name)
            shutil.copytree(result_path, new_path)
        if 100<cac_value <=400:
            out_folder_name = '中度钙化，中至高度风险'
            out_folder_name_path = os.path.join(out_folder, out_folder_name)
            if not os.path.exists(out_folder_name_path):
                os.makedirs(out_folder_name_path)
            new_path = os.path.join(out_folder_name_path, name)
            shutil.copytree(result_path, new_path)
        if cac_value >400:
            out_folder_name = '广泛钙化，极高风险'
            out_folder_name_path = os.path.join(out_folder, out_folder_name)
            if not os.path.exists(out_folder_name_path):
                os.makedirs(out_folder_name_path)
            new_path = os.path.join(out_folder_name_path, name)
            shutil.copytree(result_path, new_path)